package app.modules.cloudpivot.sso;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.authine.cloudpivot.user.domain.Account;
import com.authine.cloudpivot.web.sso.security.BaseAuthenticationToken;
import com.authine.cloudpivot.web.sso.template.BaseSimpleTemplate;
import com.authine.cloudpivot.web.sso.template.ResponseTypeEnum;
import com.authine.cloudpivot.web.sso.template.SimpleTemplateConfig;

/**
 * 免登接口
 *
 * @author LiuGangQiang Create in 2024/12/09
 */
public class ThirdpartyAuth extends BaseSimpleTemplate {
    private final static Logger LOG = LoggerFactory.getLogger(ThirdpartyAuth.class);

    protected ThirdpartyAuth() {
        super(SimpleTemplateConfig.builder()
                .requestMatcher(new AntPathRequestMatcher("/login/thirdparty", HttpMethod.GET.name()))
                .responseType(ResponseTypeEnum.JSON)
                .redirectClientID("api")
                .redisTokenKey("thirdparty_access_token")
                .redisTicketKey("thirdparty_ticker")
                .build());
    }

    @Override
    public String getSourceId(BaseAuthenticationToken token) throws Exception {
        LOG.info("===>>获取唯一标识：{}", token.getCode());
        return token.getCode();
    }

    @Override
    public Account getUser(Object sourceId) {
        return engineService.getAccountService().findBySourceId(sourceId.toString());
    }
}
